﻿package com.shangda.Server;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.Properties;
import java.util.Timer;

import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoAcceptor;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

import com.shangda.frame.MainFrame;
import com.shangda.frame.TimeOclock;
import com.shangda.service.DataService;

public class MinaServer {
	
	
	//缓冲区大小
	private	int	bufSize = 1024;
	
	//服务器端口好号
	private	int port = 8990;
	//数据库操作类
	private DataService dataService;
	
	//界面类
	private MainFrame mainFrame;
	//时间对象
    private TimeOclock o;
	
	
	private  MinaServer() {
		
		try {
			BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;
			//普通不透明边框
			//BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.
			BeautyEyeLNFHelper.launchBeautyEyeLNF();
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    	mainFrame = new MainFrame();
    	mainFrame.init();
    	
    	
    	//创建时间参数
    	o = new TimeOclock(mainFrame);
    	Timer t = new Timer();
    	t.schedule(o,1,5000);
    	
    	
    	//获取服务器端口号
    	//获取配置文件的port
    	try {
    		FileInputStream in = new FileInputStream("properties/config.properties");//文件在项目下。不是在包下！！
    		Properties p = new Properties();
    		p.load(in);//加载输入流 获得键值对
    		String serverPort = p.getProperty("port");
        	mainFrame.getServerPortField().setText(serverPort);
        	this.port = Integer.parseInt(serverPort);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    	
    	//初始化dataService
    	this.dataService = new DataService();
    	
    	
    	//初始化服务器
    	IoAcceptor acceptor = new NioSocketAcceptor();
		acceptor.setHandler(new MinaBizHandler(dataService,port,mainFrame));
		acceptor.getSessionConfig().setReadBufferSize(bufSize);
		acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
	    try {
			acceptor.bind(new InetSocketAddress(port));
		} catch (IOException e) {
			e.printStackTrace();
		}
    	

	}
	
	public static void main(String[] args) {
		MinaServer ms = new MinaServer();
	}

	
}
